#!/usr/bin/env python
#-*- coding: utf-8 -*-
#__author__:vincentlc
#time: 16/4/10 : 01:01


"""
Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
"""


# Definition for a binary tree node.
class TreeNode(object):
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution(object):
    def isBalanced(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """

        if root == None:
            return True

        flag = self.findDepth(root)

        if flag<0:
            return False

        return True



    def findDepth(self, root):
        if root == None:
            return 0
        left_depth = 0
        right_depth = 0
        # if root.left != None:
        left_depth = self.findDepth(root.left)
        # if root.right!= None:
        right_depth = self.findDepth(root.right)
        if left_depth==-1 or right_depth == -1 or abs(left_depth - right_depth)>= 2:
            return -1
        # if abs(left_depth - right_depth)>= 2:
        #     return -1
        return max(left_depth,right_depth)+1
